<template>
  <div class="login-wrap">
    <div class="login-container">
      <h1 class="title">中文店铺名称识别系统</h1>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="login-form"
      >
        <el-form-item prop="userAccount">
          <el-input
            v-model="ruleForm.userAccount"
            prefix-icon="el-icon-user"
            placeholder="请输入账号"
          />
        </el-form-item>

        <el-form-item prop="userPassword">
          <el-input
            v-model="ruleForm.userPassword"
            prefix-icon="el-icon-lock"
            type="password"
            show-password
            placeholder="请输入密码"
          />
        </el-form-item>

        <el-form-item prop="verifyCode">
          <div class="verify-code-container">
            <el-input
              v-model="ruleForm.verifyCode"
              prefix-icon="el-icon-key"
              placeholder="请输入验证码"
              class="verify-input"
            />
            <div class="verify-code" @click="refreshCode">
              {{ code }}
            </div>
          </div>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            :loading="logining"
            class="login-button"
            @click="submitForm"
          >
            {{ logining ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>

        <div class="login-footer">
          <span class="register-link" @click="register">
            没有账号？立即注册
          </span>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import * as api from '../api/interface'

export default {
  data() {
    // 验证码验证规则
    const validateVerifyCode = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入验证码'))
      } else if (value.toLowerCase() !== this.code.toLowerCase()) {
        callback(new Error('验证码错误'))
      } else {
        callback()
      }
    }


    return {
      //定义loading默认为false
      logining: false,
      //输入的数据--与实体属性名相同
      ruleForm: {
        userAccount: '',
        userPassword: '',
        verifyCode: ''
      },
      code: '',
      user: {},
      //rules前端验证，当输入框为空时
      rules: {
        userAccount: [{required: true, message: '请输入账号', trigger: 'blur'}],
        userPassword: [{required: true, message: '请输入密码', trigger: 'blur'}],
        verifyCode: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { validator: validateVerifyCode, trigger: 'blur' }
        ]
      }
    }
  },
  // 页面初始化操作
  created() {
    this.generateCode()
    console.log("init success");
  },
  // 里面的函数只有调用才会执行
  methods: { // 生成随机验证码
    generateCode() {
      const characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
      let result = ''
      for (let i = 0; i < 4; i++) {
        result += characters.charAt(Math.floor(Math.random() * characters.length))
      }
      this.code = result
    },
    // 刷新验证码
    refreshCode() {
      this.generateCode()
      this.ruleForm.verifyCode = ''
    },
    submitForm(formName) {
      //对ref="ruleForm"进行表单验证的规则
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.logining = true
          api.requestLogin(this.ruleForm).then(data => {
            this.logining = false;
            if (data.message != "success") {
              //查询不成功时设置，error为红色
              this.logining = false;
              this.$message.error(data.message)
            } else {
              this.user = data.returnObj
              //正在登陆的用户的各种数据保存到前端
              window.localStorage.setItem("userId", data.returnObj.userId)
              window.localStorage.setItem("userType", data.returnObj.userType)
              window.localStorage.setItem("userName", data.returnObj.userName)
              this.$store.commit('login', this.user)
              this.$router.push({
                //前端路径
                path: data.returnObj.userType == '1' ? '/store/storeList' : "store/storeList"
              })
            }
          }).catch((error, status) => {
            this.logining = false;
          });
        } else {
          this.$message.error('请输入用户名密码！')
          this.logining = false;
          return false;
        }
      })
    },
    register() {
      this.$router.push({ path: '/register' })
    },
  }
}
</script>

<style scoped>
.login-wrap {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #1a2a6c 0%, #b21f1f 51%, #fdbb2d 100%);
}

.login-container {
  width: 400px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
}

.title {
  margin-bottom: 40px;
  font-size: 24px;
  font-weight: 600;
  color: #1a2a6c;
  text-align: center;
}

.login-form {
  .el-form-item {
    margin-bottom: 25px;
  }

  .el-input {
    height: 42px;

    /deep/ .el-input__inner {
      height: 42px;
      line-height: 42px;
      border-radius: 4px;
      border: 1px solid #dcdfe6;

      &:focus {
        border-color: #b21f1f;
        box-shadow: 0 0 0 2px rgba(178, 31, 31, 0.1);
      }
    }
  }
}

.verify-code-container {
  display: flex;
  gap: 10px;
}

.verify-input {
  flex: 1;
}

.verify-code {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100px;
  height: 42px;
  background: linear-gradient(45deg, #1a2a6c, #b21f1f);
  color: white;
  font-family: 'Arial', sans-serif;
  font-size: 18px;
  font-weight: bold;
  letter-spacing: 2px;
  cursor: pointer;
  user-select: none;
  border-radius: 4px;
  transition: transform 0.3s ease;
}

.verify-code:hover {
  transform: scale(1.02);
}

.login-button {
  width: 100%;
  height: 42px;
  font-size: 16px;
  background: #b21f1f;
  border: none;
  border-radius: 4px;
  transition: all 0.3s ease;

  &:hover {
    background: #d42525;
    transform: translateY(-1px);
  }

  &:active {
    transform: translateY(1px);
  }
}

.login-footer {
  margin-top: 20px;
  text-align: center;
}

.register-link {
  color: #b21f1f;
  font-size: 14px;
  cursor: pointer;
  transition: color 0.3s ease;

  &:hover {
    color: #d42525;
    text-decoration: underline;
  }
}

@media screen and (max-width: 480px) {
  .login-container {
    width: 90%;
    padding: 30px 20px;
  }

  .title {
    font-size: 20px;
    margin-bottom: 30px;
  }

  .verify-code {
    width: 80px;
  }
}

.el-input__inner:focus {
  transition: all 0.3s ease;
}

.el-button.is-loading {
  position: relative;
  pointer-events: none;
  opacity: 0.8;
}
</style>
